package com.cyms.modules.order.service.impl;

import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.DateUtils;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;
import com.cyms.modules.base.entity.LineEntity;
import com.cyms.modules.base.service.LineService;
import com.cyms.modules.order.dao.OrderDao;
import com.cyms.modules.order.entity.OrderCarEntity;
import com.cyms.modules.order.entity.OrderEntity;
import com.cyms.modules.order.service.OrderCarService;
import com.cyms.modules.order.service.OrderService;
import com.cyms.modules.sys.entity.SysUserEntity;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
	@Autowired
    private LineService lineService;
	@Autowired
    private OrderCarService orderCarService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String orderNo=(String) params.get("orderNo");
    	String customerNo=(String) params.get("customerNo");
    	String customerName=(String) params.get("customerName");
    	String carCode = (String) params.get("carCode");
    	Long createOrg = (Long) params.get("createOrg");
		List<OrderCarEntity> carList = null;
		List<Long> idList = null;
		Page<OrderEntity> page = null;
    	if (StringUtils.isNotBlank(carCode)) {
    		carList = orderCarService.selectList(new EntityWrapper<OrderCarEntity>().like("car_code",carCode));
    		idList = carList.parallelStream().map(c->c.getOrderId()).collect(Collectors.toList());
    		if (idList != null && idList.size() > 0) {
				page = this.selectPage(
						new Query<OrderEntity>(params).getPage(),
						new EntityWrapper<OrderEntity>()
								.eq(createOrg != null,"create_org",createOrg)
								.like(StringUtils.isNotBlank(orderNo), "order_no",orderNo)
								.like(StringUtils.isNotBlank(customerNo), "customer_no",customerNo)
								.like(StringUtils.isNotBlank(customerName), "customer_name",customerName)
								.in("id",idList)
								.orderDesc(Collections.arrayToList(new String[]{"create_date"}))
				);
			}
		}else {
			page = this.selectPage(
					new Query<OrderEntity>(params).getPage(),
					new EntityWrapper<OrderEntity>()
							.eq(createOrg != null,"create_org",createOrg)
							.orderBy("create_date", false)
							.like(StringUtils.isNotBlank(orderNo), "order_no",orderNo)
							.like(StringUtils.isNotBlank(customerNo), "customer_no",customerNo)
							.like(StringUtils.isNotBlank(customerName), "customer_name",customerName)
			);
		}
        for (OrderEntity order : page.getRecords()) {
        	LineEntity line = lineService.selectById(order.getLineId());
        	order.setLine(line);
        	List<OrderCarEntity> orderCarList=orderCarService.selectList(new EntityWrapper<OrderCarEntity>().eq("order_id", order.getId()));
        	order.setCarList(orderCarList);
		}
        return new PageUtils(page);
    }

	@Override
	public PageUtils queryAmountPage(Map<String, Object> params) {
		String orderNo=(String) params.get("orderNo");
		String customerNo=(String) params.get("customerNo");
		String customerName=(String) params.get("customerName");
		String carCode = (String) params.get("carCode");
		Long createOrg = (Long) params.get("createOrg");
		List<OrderCarEntity> carList = null;
		List<Long> idList = null;
		Page<OrderEntity> page = null;
		if (StringUtils.isNotBlank(carCode)) {
			carList = orderCarService.selectList(new EntityWrapper<OrderCarEntity>().like("car_code",carCode));
			idList = carList.parallelStream().map(c->c.getOrderId()).collect(Collectors.toList());
			if (idList != null && idList.size() > 0) {
				page = this.selectPage(
						new Query<OrderEntity>(params).getPage(),
						new EntityWrapper<OrderEntity>()
								.eq(createOrg != null,"create_org",createOrg)
								.gt("rest_amount",0)
								.like(StringUtils.isNotBlank(orderNo), "order_no",orderNo)
								.like(StringUtils.isNotBlank(customerNo), "customer_no",customerNo)
								.like(StringUtils.isNotBlank(customerName), "customer_name",customerName)
								.in("id",idList)
								.orderDesc(Collections.arrayToList(new String[]{"rest_amount","create_date"}))
				);
			}
		}else {
			page = this.selectPage(
					new Query<OrderEntity>(params).getPage(),
					new EntityWrapper<OrderEntity>()
							.eq(createOrg != null,"create_org",createOrg)
							.gt("rest_amount",0)
							.orderBy("create_date", false)
							.like(StringUtils.isNotBlank(orderNo), "order_no",orderNo)
							.like(StringUtils.isNotBlank(customerNo), "customer_no",customerNo)
							.like(StringUtils.isNotBlank(customerName), "customer_name",customerName)
			);
		}
		for (OrderEntity order : page.getRecords()) {
			LineEntity line = lineService.selectById(order.getLineId());
			order.setLine(line);
			List<OrderCarEntity> orderCarList=orderCarService.selectList(new EntityWrapper<OrderCarEntity>().eq("order_id", order.getId()));
			order.setCarList(orderCarList);
		}
		return new PageUtils(page);
	}

	@Override
	public void batchFinishOrder(SysUserEntity user, Long[] ids) {
		// TODO Auto-generated method stub
		List<OrderEntity> orderList = this.selectBatchIds(Arrays.asList(ids));
		orderList.parallelStream().forEach(o->{
			o.setFinishAmountType("0");
			o.setFinishDate(new Date());
			o.setFinishUser(user.getUsername());
			o.setStatus("5");
			o.setAlreadyAmount(o.getTotalAmount());
			o.setRestAmount(new BigDecimal(0));
			this.updateById(o);
		});
	}

}
